class Admin::EditionsController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @edition_pages, @editions = paginate :editions, :per_page => 10
  end

  def show
    @edition = Edition.find(params[:id])
  end

  def new
    @edition = Edition.new
    @magazines = Magazine.find(:all, :order => :name).map {|c| [c.name, c.id]}
  end

  def create
    @edition = Edition.new(params[:edition])
    if @edition.save
      #Gera uma lista de prioridade para a edicao recem criada
      magazine_id = params[:edition][:magazine_id]
      edition_number = params[:edition][:number]
      next_user = PriorityList.create(magazine_id, edition_number)
      flash[:notice] = 'Edi&ccedil;&atilde;o de revista criada com sucesso!<br/>Mandar revista para: ' + next_user  +  '<br/>'
      redirect_to :action => 'list'
    else
      flash[:notice] = 'Edi&ccedil;&atilde;o de revista nao foi criada!<br/>'
      redirect_to :action => 'new'
    end
  end

  def edit
    @edition = Edition.find(params[:id])
    @magazines = Magazine.find(:all, :order => :name).map {|c| [c.name, c.id]}
  end

  def update
    @edition = Edition.find(params[:id])
    if @edition.update_attributes(params[:edition])
      flash[:notice] = 'Edi&ccedil;&atilde;o de revista atualizada com sucesso!<br/><br/>'
      redirect_to :action => 'show', :id => @edition
    else
      render :action => 'edit'
    end
  end

  def destroy
    Edition.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
